package grailsforum

class CoursesController {

    static allowedMethods = [

            deleteCourse: 'POST'
    ]

    def index() {

        [courses : Course.findAll()]
    }

    def view(){

        def c = Course.get(params.id)

        if(c){
            [course: c]
        }
        else{
            redirect action: 'index'
        }
    }

    def add(){

    }

    def doAdd(){

        Course course = new Course(params);

        if (!course.save()){

            flash.message = message(error: course.errors.allErrors.first())

            render (view: 'add')
            return
        }

        redirect(controller: 'courses', action: 'view', params: [id : Course.last().id])
    }

    def deleteCourse(){

        Course course = Course.get(params.course)
        course.delete()

        render 'ok'
    }
}
